package com.hejian.android.a72ggametools.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.activity.ActionActivity;
import com.hejian.android.a72ggametools.adapter.ActionAdapter;
import com.hejian.android.a72ggametools.adapter.ActionAdvAdapter;
import com.hejian.android.a72ggametools.asynctask.DownloadImageAsyncTask;
import com.hejian.android.a72ggametools.bean.AdvItem;
import com.hejian.android.a72ggametools.bean.Advert;
import com.hejian.android.a72ggametools.common.Constant;
import com.hejian.android.a72ggametools.imageloader.DoubleCache;
import com.hejian.android.a72ggametools.imageloader.ImageCache;
import com.hejian.android.a72ggametools.imageloader.ImageLoader;
import com.hejian.android.a72ggametools.net.HttpUtils;
import com.hejian.android.a72ggametools.utils.JsonParse;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016/1/11.
 */
public class ActionFragment extends Fragment{
    private static final String TAG = "hejiandebug";
    private static final int TIMERS = 1;
    private View view;
    private int mPosition;
    private ViewPager actionViewPager;
    private ListView actionListView;
    private List<AdvItem> advItemList = new ArrayList<AdvItem>();
    private ArrayList<ImageView> viewList;
    private ImageView imageView1;
    private ImageView imageView2;
    private ActionAdvAdapter advAdapter;
    private ImageLoader imageLoader;
    private ImageCache imageCache = new DoubleCache();

    private Handler timerHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == TIMERS) {
                mPosition++;
                if (mPosition >= actionViewPager.getChildCount()) {
                    mPosition = 0;
                }
                actionViewPager.setCurrentItem(mPosition);
            }
        }
    };
    private ImageView imgDot1;
    private ImageView imgDot2;
    private TextView advTitle;
    private List<Advert> list;
    private TimerTask timerTask;

    @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_action, null);

        initView();
        initData();
        setAdapter();
        setListener();


        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        initTimeTask();
    }

    private void initTimeTask() {
        timerTask = new TimerTask() {
            @Override
            public void run() {
                Message msg = Message.obtain();
                msg.what = TIMERS;
                timerHandler.sendMessage(msg);
            }
        };
    }


    private void setListener() {
        setViewPagerListener();
        setItemListener();
    }



    private void setItemListener() {
        actionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ActionActivity.class);
                intent.putExtra("id",advItemList.get(position).getId());
                startActivity(intent);
            }
        });
    }

    private void setViewPagerListener() {
        actionViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mPosition = position;
                setCurrentDot(position);
                advTitle.setText(list.get(position).getBname());

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setAdapter() {
        advAdapter = new ActionAdvAdapter(viewList);
        actionViewPager.setAdapter(advAdapter);

    }

    private void initView() {
        actionViewPager = (ViewPager) view.findViewById(R.id.action_viewpager);
        actionListView = (ListView) view.findViewById(R.id.action_listview);
        viewList = new ArrayList<ImageView>();
        imageView1 = new ImageView(getActivity());
        imageView2 = new ImageView(getActivity());
        imageView1.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView2.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageLoader = ImageLoader.getInstance(getActivity(), imageCache);
        imgDot1 = (ImageView) view.findViewById(R.id.action_viewpager_dot_01);
        imgDot2 = (ImageView) view.findViewById(R.id.action_viewpager_dot_02);
        advTitle = (TextView) view.findViewById(R.id.adv_title);


    }

    private void initData() {
        Log.d(TAG, "initData: ...............");
        initAdvData();
        initAdvListData();


    }

    private void initAdvListData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadAdvList();
            }
        }).start();
    }


    private void initAdvData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadAdvImge();
            }
        }).start();


    }

    private void loadAdvList() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("platform", "2");
        String buffer = HttpUtils.doPostRequest(Constant.ACTION_LIST_URL, map);
        Log.d(TAG, "loadAdvList: " + buffer);
        List<AdvItem> list = JsonParse.parseAdvListJson(buffer);
        Message msg = Message.obtain();
        msg.what = 2;
        msg.obj = list;
        advItemList = list;
        Log.d(TAG, "loadAdvList: " + advItemList.size());
        mHandler.sendMessage(msg);

    }

    private void loadAdvImge() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("platform", "2");
        map.put("pos", "1");
        String buffer = HttpUtils.doPostRequest(Constant.ACTION_ADV_URL, map);
        list = JsonParse.parseAdvertJson(buffer);
        imageLoader.displayImage(list.get(0).getBimp(), imageView1);
        imageLoader.displayImage(list.get(1).getBimp(), imageView2);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = list.get(0).getId();
                Intent intent = new Intent(getActivity(),ActionActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = list.get(1).getId();
                Intent intent = new Intent(getActivity(),ActionActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
        viewList.add(imageView1);
        viewList.add(imageView2);

        Log.d(TAG, "loadAdvImge: " + list.size());
        Message msg = Message.obtain();
        msg.what = 1;
        mHandler.sendMessage(msg);

    }

    private int count = 0;
    private ActionAdapter actionAdapter;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {

                advAdapter.notifyDataSetChanged();
                Timer timer = new Timer(true);
                timer.schedule(timerTask, 300, 3000);
            } else if (msg.what == 2) {
                actionAdapter = new ActionAdapter(getActivity(), advItemList);
                actionListView.setAdapter(actionAdapter);
            }
        }
    };

    private void setCurrentDot(int position) {
        switch (position) {
            case 0:
                imgDot1.setImageResource(R.mipmap.ic_banner_dot_focus);
                imgDot2.setImageResource(R.mipmap.ic_banner_dot);
                break;
            case 1:
                imgDot2.setImageResource(R.mipmap.ic_banner_dot_focus);
                imgDot1.setImageResource(R.mipmap.ic_banner_dot);
                break;
        }
    }



}
